//
//  NetworkHandle.m
//  UI_Block
//
//  Created by Scott on 16/1/21.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "NetworkHandle.h"

@implementation NetworkHandle

+ (void)networkOperationWithUrl:(NSString *)str completeHandle:(void(^)(id result))block {
    
    NSString *codeStr = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSURL *url = [NSURL URLWithString:codeStr];
    
    
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        // 返回主线程.
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (data) {
                
                id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                
                block(result);
                
            }
        });
        
    }];
    
    [dataTask resume];
    
}



@end
